﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Fred.Services
{
    /// <summary>
    /// Fred Service base class
    /// </summary>
    internal abstract class Service : BackgroundWorker 
    {
        public Service()
        {
            DoWork += Service_DoWork;
        }

        private void Service_DoWork(object sender, DoWorkEventArgs e)
        {
            // Sleep implemented here, to prevent a busy loop no matter what the implementing class does
            Sleep(100);
        }

        protected void Sleep(int milleSeconds)
        {
            if (CancellationPending)
                return;

            for (int left = milleSeconds; left > 0; left -= 100 )
            {
                Thread.Sleep(100);
            }
        }
    }
}
